package com.flot.yazb.widget.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;


/**
 * Toast工具类
 *
 */
public class ToastUtil {

    private static Toast mToast;

    /**
     * 打印toast方法
     *
     * @param context
     * @param msg
     */
    public static void showToast(Context context, String msg) {
		/*Toast toast = Toast.makeText(context, msg, 0);
		//toast.setGravity(Gravity.CENTER, 0, -60);
		toast.show();*/
        showToast(context, msg, 0);
    }

    /**
     * 重载打印toast
     *
     * @param context
     * @param msg
     */
    public static void showToast(Context context, int msg) {
		/*Toast toast = Toast.makeText(context, msg, 0);
		toast.setGravity(Gravity.CENTER, 0, -60);
		toast.show();*/
        showToast(context, msg, 0);
    }

    /**
     * 打印自定义的toast
     *
     * @param context
     * @param resouce
     *            背景图片
     */
    public static void showCustomToast(Context context, int resouce) {
        Toast toast = new Toast(context);
        ImageView imageView = new ImageView(context);
        imageView.setBackgroundResource(resouce);
        toast.setGravity(Gravity.CENTER, 0, -60);

        toast.setView(imageView);
        toast.show();

    }

    /**
     * 打印自定义toast 这个地方是一直要显示toast 避免toast太多。。
     *
     * @param context
     *            上下文
     * @param layoutId
     *            要显示的layout的id
     */
    public static void showCustomToastByLayout(Context context, int layoutId) {
        View view = View.inflate(context, layoutId, null);
        mToast = new Toast(context);
        mToast.setGravity(Gravity.CENTER, 0, -60);
        mToast.setView(view);
        mToast.setDuration(50);
        mToast.show();
    }

    /**
     * 避免多次出现提示，提示在屏幕持续很久
     *
     * @param context
     * @param msg
     * @param duration
     */
    public static void showToast(Context context, String msg, int duration) {
        if (mToast == null) {
            mToast = Toast.makeText(context, msg, duration);
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }

    /**
     * 避免多次出现提示，提示在屏幕持续很久
     *
     * @param context
     * @param msg
     * @param duration
     */
    public static void showToast(Context context, int msg, int duration) {
        if (mToast == null) {
            mToast = Toast.makeText(context, msg, duration);
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }

    /**
     * 关闭toast
     */
    public static void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }

}